package com.wang.tank;

import java.io.*;
import java.util.Vector;

/**
 * @author 王先锋
 * @version 1.0
 */
public class Recorder {
    //定义变量记录击毁坦克数
    private static int allEnemyTankNum = 0;
    //定义IO对象,准备些数据当文件中
    private static BufferedWriter bw = null;
    //记录文件保存到src下面
    private static String recordFile = "src\\myRecord.txt";
    private  static BufferedReader br=null;
    private static Vector<EnemTank> enemyTanks = null;
    //定义一个Node的vecoter记录敌人信息
    private  static  Vector<Node> nodes=new Vector<>();

    public static String getRecordFile() {
        return recordFile;
    }

    public static void setEnemyTanks(Vector<EnemTank> enemyTanks) {
        Recorder.enemyTanks = enemyTanks;
    }

    public static  Vector<Node>getNodesAndEnemyTankRec(){
        try {
            br=new BufferedReader(new FileReader(recordFile));
        allEnemyTankNum=Integer.parseInt(br.readLine());
       String line="";
       while((line=br.readLine())!=null){
          String[] xyd=line.split(" ") ;
          Node node=new Node(Integer.parseInt(xyd[0]),Integer.parseInt(xyd[1]),
                  Integer.parseInt(xyd[2]));
          nodes.add(node);
       }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        return  nodes;
    }
    //增加方法，当游戏退出时，我们将allEnemyTankNum保存到recordFile
    //保存敌人坐标坐标方向
    public static void keepRecord() {
        try {
            bw = new BufferedWriter(new FileWriter(recordFile));
            bw.write(allEnemyTankNum + "\r\n");
            //00p,定义一个属性，然后通过setXXX得到敌人坦克的vector
            for (int i = 0; i < enemyTanks.size(); i++) {
                EnemTank enemTank = enemyTanks.get(i);
                if (enemTank.isLive) {
                    //保存坦克信息
                    String record = enemTank.getX() + " " + enemTank.getY() + " " + enemTank.getDirect();
                    //写入文件
                    bw.write(record + "\r\n");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }


    public static int getAllEnemyTankNum() {
        return allEnemyTankNum;
    }

    public static void setAllEnemyTankNum(int allEnemyTankNum) {
        Recorder.allEnemyTankNum = allEnemyTankNum;
    }

    //当我方击毁一个就加加
    public static void addallEnemyTankNum() {
        Recorder.allEnemyTankNum++;
    }


}
